<?php 
session_start();
include("../cartlibs.php");
include "../../dbcon.php";


	$idDH = $_SESSION['UserName'].date("U"); // ket hop username + thoi gian>>> ko bao gio trung id don hang
	// date u tinh tu 0h0phut0s ngay 1/1/1970 den hien tai.
	$user = $_SESSION['UserName'];

	$ngaydat = $_POST['f_calcdate'];
	$ngaygiao = $_POST['f_date_a'];
	$nguoinhan = $_POST['tennguoinhan'];
	$diadiem = $_POST['diadiem'];
	$donhang = "INSERT INTO donhang(idDH,UserName,ThoiDiemDatHang,ThoiDiemGiaoHang,TenNguoiNhan,DiaDiemGiaoHang) VALUES ('$idDH','$user','$ngaydat','$ngaygiao', '$nguoinhan', '$diadiem')";
	if(mysql_query($donhang,$link))
		{
			$max=count($_SESSION["GioHang"]); // lay so mat hang trong gio hang
			for($i=0;$i<$max;$i++)
			{	
				$UrlHinh=$_SESSION["GioHang"][$i]["UrlHinh"];
				$MaSP=$_SESSION["GioHang"][$i]["MaSP"];
				$SoLuong=$_SESSION["GioHang"][$i]["SoLuong"];
				$Gia=$_SESSION["GioHang"][$i]["Gia"];
				if($SoLuong==0)
				continue;
					
					$donhangct = "INSERT INTO donhangchitiet(idDH,MaSP,SoLuong,DonGia) VALUES ('$idDH','$MaSP','$SoLuong','$Gia')";
					mysql_query($donhangct,$link);
					echo "Mua hang thanh cong.<a href='../../index.php'>Quay lai trang chu</a>";
			}
		}

?>
